package com.developerstation.barcode.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.developerstation.barcode.mapper.CkaMapper;
import com.developerstation.barcode.mapper.CkaTmMapper;
import com.developerstation.barcode.model.Cka;
import com.developerstation.barcode.model.CkaExample;
import com.developerstation.barcode.model.CkaTm;
import com.developerstation.barcode.model.CkaTmExample;
import com.developerstation.barcode.service.TmService;

@Service
public class TmServiceImpl implements TmService{
	private static final Log log = LogFactory.getLog(TmServiceImpl.class);
	@Resource
	private CkaTmMapper ckaTmMapper;
	
	
	@Resource
	private CkaMapper ckaMapper;
	
	@Override
	public int insert(CkaTm record) {
		ckaTmMapper.insert(record);
		return 0;
	}

	@Override
	public int insertSelective(CkaTm record) {
		ckaTmMapper.insertSelective(record);
		return 0;
	}

	@Override
	public int insertForBatch(List<CkaTm> records) {
		if(records==null || records.size()<=0) return 0;
		return ckaTmMapper.insertTmForBatch(records);
	}


	@Override
	public void insertList(List<CkaTm> records) {
		if(records==null || records.size()<=0) return;
		for (CkaTm ckaTm : records) {
			ckaTmMapper.insertSelective(ckaTm);
		}
	}
	
	@Override
	public void execTmStatisticCallable(int[] tmIds) {
		if(tmIds==null || tmIds.length<=0) return;
		Map paramMap = new HashMap();
		for (int i = 0; i < tmIds.length; i++) {
			paramMap.put("cka_tm_id", tmIds[i]);
			ckaTmMapper.updateTmStatisticCallable(paramMap);
		}
	}

	@Override
	public void execTmStatusCallable(int[] tmIds,int flag,String paramChar) {
		Map paramMap = new HashMap();
		for (int i = 0; i < tmIds.length; i++) {
			paramMap.put("cka_tm_id", tmIds[i]);
			paramMap.put("flag", flag);
			paramMap.put("paramChar", paramChar);
			ckaTmMapper.updateTmStatusCallable(paramMap);
		}
	}

	@Override
	public void insertUploadAndExecCallable(List<CkaTm> records) {
		if(records==null || records.size()<=0) {
			log.info("-------->>>>records is" + records==null?"null":"size = 0 ");
			return;
		}
		//根据客户端单据修改状态 执行
		//未上传：3  已修改：如果不允许重复 1.2.3   已修改：如果允许重复2.3
		//1.更新条码状态Pr_xx_tm_Update & add(id) & 1
		//2.删除该单据的xx_cka_tm
		//3.上传xx_cka_tm
		log.info("------------->>>根据客户端单据修改状态 执行 1 2 3 ");
		
		List<Cka> qcList =  getqc(records);
		for (Cka cka : qcList) {
			int updateStatus = cka.getUpdateStatus();
			boolean repetition = cka.getRepetition();
			executeBeforeByUpdateStatus(cka, updateStatus, repetition);
		}
		
		this.insertList(records);
		//执行存储过程
		for (CkaTm ckaTm : records) {
			log.info("------------->>>执行存储过程 ckaTm id= " + ckaTm.getId());
			if(ckaTm.getRepetition()==1){
				this.execTmStatisticCallable(new int[]{ckaTm.getId()});
			}else if(ckaTm.getRepetition()==0){
				this.execTmStatisticCallable(new int[]{ckaTm.getId()});
				this.execTmStatusCallable(new int[]{ckaTm.getId()},2,"");
			}
		}
		
	}

	private void executeBeforeByUpdateStatus(Cka cka, int updateStatus,
			boolean repetition) {
		log.info("------------->>>start executeBeforeByUpdateStatus");
		//已修改不允许重复
		if(updateStatus == 2 && repetition==false){
			log.info("------------->>>已修改不允许重复 updateStatus=" + updateStatus +",repetition="+repetition);
			CkaTmExample example = new CkaTmExample();
			example.createCriteria().andCkEqualTo(cka.getCk())
									.andNoEqualTo(cka.getNo());
			List<CkaTm> ckaTmList = ckaTmMapper.selectByExample(example);
			if(ckaTmList == null || ckaTmList.size()<=0){
				return;
			}
			
			List<Integer> tmIds = new ArrayList<Integer>();
			for (CkaTm ckaTm : ckaTmList) {
				this.execTmStatusCallable(new int[]{ckaTm.getId()},1,"");
				tmIds.add(ckaTm.getId());
			}
			example.clear();
			example.createCriteria().andIdIn(tmIds);
			ckaTmMapper.deleteByExample(example);
		
		//已修改允许重复
		}else if(updateStatus == 2 && repetition==true){
			log.info("------------->>>已修改允许重复 updateStatus=" + updateStatus +",repetition="+repetition);
			CkaTmExample example = new CkaTmExample();
			example.createCriteria().andCkEqualTo(cka.getCk())
									.andNoEqualTo(cka.getNo());
			ckaTmMapper.deleteByExample(example);
		}
	}

	//得到去重单据
	private List<Cka> getqc(List<CkaTm> records) {
		List<Cka> ckaList = new ArrayList<Cka>();
		Set<String> noSet = new HashSet<String>();
		for (CkaTm ckaTm : records) {
			String no = ckaTm.getNo();
			if(!noSet.contains(no)){
				noSet.add(no);
				Cka cka = new Cka();
				cka.setNo(ckaTm.getNo());
				cka.setCk(ckaTm.getCk());
				cka.setRepetition(ckaTm.getRepetition()==0?false:true);
				cka.setUpdateStatus(ckaTm.getUpdateStatus());
				ckaList.add(cka);
			}
		}
		return ckaList;
	}

	@Override
	public List<Cka> getUnPostedCk(String ck,String[] noIdArray) {
		if(noIdArray == null){
			return null;
		}
		List<String> values = Arrays.asList(noIdArray);
		CkaExample example = new CkaExample();
		example.createCriteria().andNoIn(values)
								.andCkEqualTo(ck)
								.andPostedEqualTo(false);
		List<Cka> ckaList = ckaMapper.selectByExample(example);
		
		return ckaList;
	}
	
}
